package app.demo;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.util.Hashtable;
import java.util.Vector;

import app.kernel.api.McsAPI;
import config.PSG_Configuration;

public class demoExperiment {

	// To keep track of time.
	public static double curr;
	public static int Count=4;
	public static int maxN = 100;
	public static int Ndiscoverer = 1;
	public static Hashtable PSGs = new Hashtable();
	public static String defaultIP =PSG_Configuration.defaultIP;
	public static int defaultPort = PSG_Configuration.defaultPort;
	public static String defaultName =PSG_Configuration.PSGName;
	public static Vector myTimings = new Vector();
	public static Vector myComments = new Vector();
	public static float recallTotal =0;
	
	/**
	 * @param args
	 */
	public static void main(String[] args) throws Exception {

		String queries [] = { "SELECT name from shop where name = \"shop160\"",
                "SELECT hasAdvertisement from shop where hasAdvertisement = \"true\"",
                "SELECT crowd_level from shop where crowd_level = \"low\"",
                "SELECT egg_loc from shop where egg_loc = \"Basement 1\"",
                "SELECT egg_price from shop where egg_price = \"2\"",
				            };
		
		// TODO Auto-generated method stub
		maxN = 10; // Number of Queries sent.
		int Ndiscoverer = 1;
		int calib = 0;
		curr = System.currentTimeMillis();
	    System.out.println("Start " + "Time: "+curr);			

		String ID_KEY = "http://"+defaultIP+ ":"+defaultPort+"/xmlrpc"; 
	    PSGs.put(ID_KEY, new McsAPI(defaultName, defaultIP,defaultPort));
	
	    int TTL = 30;
	    
	    // Test Request and Response time.	
	    for (int i=0; i<maxN; i++) {
			
			System.out.println(((McsAPI)PSGs.get(ID_KEY)).queryMCS(ID_KEY,queries[0],(TTL)));
		    System.out.println("Start Waiting..");
			Thread.sleep(15000);
			print_Timings("Range Cluster (R)esponse Time 400 Peers - 5 Machines - TTL "+(TTL)+" 01% Peers (0)");
			System.out.println("Recall Average = "+(recallTotal/(i+1)));
			myComments = new Vector();
		}

	    
	    
/*	    
	    double time = (System.currentTimeMillis()-curr);				    
	    System.out.println("                 Time Taken: "+ time);
	    System.out.println("Request-Response Per Second: "+ 1000/(time/demo.maxN/demo.Ndiscoverer));*/
		
	}
	
	public static void print_Timings(String Remarks) throws Exception{
		
		int total = 0;
		java.util.Date myDate = new java.util.Date();
		myDate.setTime(System.currentTimeMillis());
		(new File("June/"+Remarks+"/")).mkdirs();
		String Filename =Remarks+"/("+myDate.toGMTString().substring(0, myDate.toGMTString().length()-13)+" "+myDate.getHours()+"hrs "+myDate.getMinutes()+"min "+myDate.getSeconds()+"sec)"+".txt"; 
		System.out.println("Writing results to Log File: log/"+Filename);
	
		
		BufferedWriter out = new BufferedWriter(new FileWriter("June/"+Filename));				
		out.write("["+maxN+" Test Runs on "+Remarks+"]\n");
		
		System.out.println("["+maxN+" Test Runs on "+Remarks+"]");
		
		for (int i=0; i<3; i++){
			
			out.write((String)myComments.get(i)+" ms\n");
			
		}
		
		out.write("\n[P2P Query Flooding]\n");
		for (int i=0; i<myTimings.size(); i++){			
			total+=((Double)myTimings.get(i)).doubleValue();
			out.write(" "+(i+1)+". " +myTimings.get(i)+" ms\n");
			
		}
		out.write("Average Timing = "+(total/myTimings.size())+" ms\n");
		
		System.out.println("Average Timings = "+(total/myTimings.size())+" ms");
		out.close();
		System.out.println("Written results to Log File: log/"+Filename);
	}
	
	public static void print_TTL(String Remarks, int iter) throws Exception{
		
		int total = 0;
		java.util.Date myDate = new java.util.Date();
		myDate.setTime(System.currentTimeMillis());
		(new File("June/"+Remarks+"/")).mkdirs();
		String Filename =Remarks+"/("+myDate.toGMTString().substring(0, myDate.toGMTString().length()-13)+" "+myDate.getHours()+"hrs "+myDate.getMinutes()+"min "+myDate.getSeconds()+"sec)"+".txt"; 
		System.out.println("Writing results to Log File: log/"+Filename);
	
		
		BufferedWriter out = new BufferedWriter(new FileWriter("June/"+Filename));				
		out.write("["+maxN+" Test Runs on "+Remarks+"]\n");
		
		System.out.println("["+maxN+" Test Runs on "+Remarks+"]");
		
		for (int i=0; i<3; i++){
			
			out.write((String)myComments.get(i)+" ms\n");
			
		}
		System.out.println("MyCOMMENTS SIZE = "+myComments.size());
		out.write("Peers Reporting Back: "+(myComments.size()-3)+"\n");
		
		float recall = ((float)(myComments.size()-3))*5/4; 
		System.out.println("Recall = "+recall+"%");
		recallTotal+=recall;
		
		out.write("Recall = "+recall+"%");
		out.write("\nRecall Avg. = "+(recallTotal/iter)+"%");
		
		out.write("\n[P2P Query Flooding]\n");
		for (int i=0; i<myTimings.size(); i++){			
			total+=((Double)myTimings.get(i)).doubleValue();
			out.write(" "+(i+1)+". " +myTimings.get(i)+" ms\n");
			
		}
		if (myTimings.size()>0){
			out.write("Average Timing = "+(total/myTimings.size())+" ms\n");
			System.out.println("Average Timings = "+(total/myTimings.size())+" ms");
		}
		else{
			out.write("Average Timing = N.A. \n");
			System.out.println("Average Timing = N.A. \n");
		}
		out.close();
		System.out.println("Written results to Log File: log/"+Filename);
	}

}
